package com.hy.mydesktop.client.mvc.core.event.manager;

import java.util.HashMap;
import java.util.Map;

import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Component;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.meta.ShortcutModelEnum;
import com.hy.mydesktop.client.component.mvc.event.EventStructureMetaModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtAppEventType;
import com.hy.mydesktop.shared.persistence.domain.gxt.ItemOfAssociateToWindow;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.ComponentMetaDataConstants;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.MetaDataConstants;
import com.hy.mydesktop.shared.util.log.MyLoggerUtil;

public class ComponentTrrigerTemplateUtil {
	
	
	public static AppEvent createAppEvent(Component sourceComponent,ItemOfAssociateToWindow windowItem) {
		AppEvent appEvent=null;
		if(windowItem.getRegisterEventType()!=null){
			
			GxtAppEventType gxtAppEventType = windowItem.getRegisterEventType();
			if (gxtAppEventType.getId()==null) {
				MyLoggerUtil.printWarn(ComponentTrrigerTemplateUtil.class,
						 sourceComponent.getClass().getName()+"【id="
						 +sourceComponent.getData(ComponentMetaDataConstants.COMPONENT_ID)
						 +"】，没有 注册触发的AppEvents"+
						 "事件;");
				return null;
			}
			final Map<String,EventStructureMetaModel> appEventCodes = new HashMap<String, EventStructureMetaModel>();
			appEventCodes.put(gxtAppEventType.getId(), windowItem.getEventStructureMetaModel());
			for ( final String code : appEventCodes.keySet()) {
				Integer i = Integer.valueOf(code);
				EventStructureMetaModel eventstructuremetamodel = appEventCodes	.get(code);

				switch (i) {
		            case 1://Integer.valueOf("0001")
		            	//shortcutController.addRegisterEventType(AppEvents.Login);
		            	
		                break;
		            case 2://Integer.valueOf("0002")
		            	//shortcutController.addRegisterEventType(AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER);	
		                break;
		            case 3://Integer.valueOf("0003")
		            	
		                break;
		            case 401://Integer.valueOf("0401")AppEvents.LOAD_DATA_EVENT
		            	printComponentDebugForEventTrigger(sourceComponent,eventstructuremetamodel);

		            	appEvent = new AppEvent(	AppEvents.LOAD_DATA_EVENT);
						appEvent =setEventData(appEvent,sourceComponent,eventstructuremetamodel,windowItem);

		                break;
		            case 5://Integer.valueOf("0101")	AppEvents.DESKTOP_MENUITEM_ONCLICK_INIT	            	

		            	printComponentDebugForEventTrigger(sourceComponent,eventstructuremetamodel);
	//					MyLoggerUtil.printComponentDebugForEventTrigger(ComponentTrrigerTemplateUtil.class,						 
						appEvent = new AppEvent(	AppEvents.DESKTOP_MENUITEM_ONCLICK_INIT);
						appEvent =setEventData(appEvent,sourceComponent,eventstructuremetamodel,windowItem);
						
						
		                break;
		            case 7://Integer.valueOf("0007")	AppEvents.DESKTOP_WINDOWAPPLICATION_INIT            	

		            	printComponentDebugForEventTrigger(sourceComponent,eventstructuremetamodel);
	//					MyLoggerUtil.printComponentDebugForEventTrigger(ComponentTrrigerTemplateUtil.class,						 
						appEvent = new AppEvent(	AppEvents.DESKTOP_WINDOWAPPLICATION_INIT);
						appEvent =setEventData(appEvent,sourceComponent,eventstructuremetamodel,windowItem);
						
						
		                break;
	           		           
		            default:
		                break;
		        }
			}
						
		
		}
		
		return appEvent;
	}

	private static AppEvent setEventData(AppEvent appEvent, Component sourceComponent,
			EventStructureMetaModel eventstructuremetamodel,
			ItemOfAssociateToWindow item) {
		appEvent.setData(	ComponentMetaDataConstants.EVENT_STRUCTURE_METAMODEL,eventstructuremetamodel);
		appEvent.setData(	ComponentMetaDataConstants.SOURCE_COMPONENT,sourceComponent);
		appEvent.setData(   MetaDataConstants.GXT_TO_SEAM_SERVICEPARAMETER_MODEL,
				item.getGxtToSeamServiceParameter());
		appEvent.setSource(sourceComponent);
		return appEvent;
		
	}

	private static void printComponentDebugForEventTrigger(
			Component sourceComponent,
			EventStructureMetaModel eventstructuremetamodel) {
		int lastIndexOfPoint= MyLoggerUtil.getlastIndexOfPoint(sourceComponent.getClass().getName());

		MyLoggerUtil.printInfo(ComponentTrrigerTemplateUtil.class,
				 sourceComponent.getClass().getName().substring(1+lastIndexOfPoint)+"【id="
				 +sourceComponent.getData(ComponentMetaDataConstants.COMPONENT_ID)
				 +"】，触发AppEvents.DESKTOP_MENUITEM_ONCLICK_INIT"+
				 "事件;" +
				 "目标控制器【rootControllerId=" +eventstructuremetamodel.getRootControllerId()+
				 "】,【targetControllerId=" +eventstructuremetamodel.getControllerId()+
				 "】");
		
	}

}
